import json
import requests

from app.libs.im_sign import TLSSigAPIv2


def friendsapiurl():
    api = TLSSigAPIv2(1400290239, '90ad15f77c6bd9b1b24977bdd40b6a09a70a907ddc1a83e9e087c40d0565c522')
    sign = api.gen_sig("xiaohengheng")
    url = f'https://console.tim.qq.com/v4/sns/friend_add?sdkappid=1400290239&identifier=xiaohengheng&usersig={sign}&random=99999999&contenttype=json'
    data = {
        "From_Account": "test1",
        "AddFriendItem":
            [
                {
                    "To_Account": "2950",
                    "Remark": "remark1",
                    "GroupName": "同学",
                    "AddSource": "AddSource_Type_XXXXXXXX",
                    "AddWording": "I'm Test1"
                }
            ],
        "AddType": "Add_Type_Both",
        "ForceAddFlags": 1
    }
    r = requests.post(url, json=data)
    print(r.text['ActionStatus'])


def sendmsgapiurl():
    api = TLSSigAPIv2(1400290239, '90ad15f77c6bd9b1b24977bdd40b6a09a70a907ddc1a83e9e087c40d0565c522')
    sign = api.gen_sig("xiaohengheng")
    url = f'https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=1400290239&identifier=xiaohengheng&usersig={sign}&random=99999999&contenttype=json'
    data = {
        "SyncOtherMachine": 1,
        "From_Account": "1236",
        "To_Account": "2950",
        "MsgRandom": 1287657,
        "MsgTimeStamp": 1557387418,
        "MsgBody": [
            {
                "MsgType": "TIMTextElem",
                "MsgContent": {
                    "Text": "我在睡觉我在睡觉我在睡觉2"
                }
            }
        ]
    }
    r = requests.post(url, json=data)
    print(r.text)


class IMoperator(object):
    appId = 1400290239
    token = '90ad15f77c6bd9b1b24977bdd40b6a09a70a907ddc1a83e9e087c40d0565c522'
    admin_account = 'xiaohengheng'

    def create_account(self, imId, nickname):
        api = TLSSigAPIv2(self.appId, self.token)
        sign = api.gen_sig(self.admin_account)
        url = f'https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid=1400290239&identifier=xiaohengheng&usersig={sign}&random=99999999&contenttype=json'
        data = {
            "Identifier": imId,
            "Nick": nickname,
            "FaceUrl": "https://samereport.oss-cn-beijing.aliyuncs.com/user_2/rc-upload-1573363466790-2_BiazfanxmamNRoxxVxka.png?x-oss-process=image/circle,r_1000"
        }
        r = requests.post(url, json=data)
        res = json.loads(r.text)
        if res['ActionStatus'] == 'OK':
            return True
        return False

    def check_account(self, item):
        api = TLSSigAPIv2(self.appId, self.token)
        sign = api.gen_sig(self.admin_account)
        url = f'https://console.tim.qq.com/v4/im_open_login_svc/account_check?sdkappid=1400290239&identifier=xiaohengheng&usersig={sign}&random=99999999&contenttype=json'
        data = item
        r = requests.post(url, json=data)
        res = json.loads(r.text)
        return res


if __name__ == '__main__':
    im = IMoperator()
    item = {
        "CheckItem":
            [
                {
                    "UserID": "writer_16073"
                }
            ]
    }
    print(im.check_account(item))
